<template>
  <div class="third-comp">
    <div class="left">
      <el-card shadow="hover">
        <template #header>
          <div class="css-2">关键词搜索</div>
        </template>
        <div class="charts">
          <div class="left-chart">
            <div class="title">搜索用户量</div>
            <div class="value">933</div>
            <v-chart :option="option1"></v-chart>
          </div>
          <div class="right-chart">
            <div class="title">搜索量</div>
            <div class="value">10626</div>
            <v-chart :option="option2"></v-chart>
          </div>
        </div>
        <div class="table">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column prop="rank" label="排名" width="60" />
            <el-table-column prop="keyWord" label="关键词" align="center" />
            <el-table-column
              prop="totalSearch"
              label="总搜索量"
              align="center"
            />
            <el-table-column
              prop="totalUser"
              label="搜索用户量"
              align="center"
            />
          </el-table>
          <el-pagination
            background
            layout="prev, pager, next"
            :total="20"
            :page-size="pageSize"
            @current-change="handleTableChange"
          />
        </div>
      </el-card>
    </div>
    <div class="right">
      <el-card shadow="hover">
        <template #header>
          <div class="css-2">分类销售排行</div>
          <div>
            <el-radio-group v-model="radio" @input="handleRadio">
              <el-radio-button label="品类" />
              <el-radio-button label="商品" />
            </el-radio-group>
          </div>
        </template>
        <div class="chart">
          <v-chart :option="option3"></v-chart>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getKeywordData, getCategoryData } from '../../api'
export default {
  data() {
    return {
      option1: {},
      option2: {},
      option3: {},
      tableData: [],
      totalData: [],
      pageSize: 6,
      radio: '品类',
      catagoryData: {},
    }
  },
  methods: {
    handleRadio(radio) {
      console.log(radio)
      if (radio === '品类') {
        this.renderChart3(this.catagoryData.data1)
      } else {
        this.renderChart3(this.catagoryData.data2)
      }
    },
    handleTableChange(index) {
      this.tableData = this.totalData.slice(
        this.pageSize * (index - 1),
        this.pageSize * index
      )
    },
    renderChart1(data) {
      this.option1 = {
        xAxis: {
          type: 'category',
          show: false,
          boundaryGap: false,
        },
        yAxis: {
          type: 'value',
          show: false,
        },
        grid: {
          left: 0,
          right: 0,
          bottom: 0,
          top: 0,
        },
        series: [
          {
            type: 'line',
            data,
            itemStyle: {
              opacity: 0,
            },
            areaStyle: {
              color: 'skyblue',
            },
            smooth: true,
          },
        ],
      }
    },
    renderChart2(data) {
      this.option2 = {
        xAxis: {
          type: 'category',
          show: false,
          boundaryGap: false,
        },
        yAxis: {
          type: 'value',
          show: false,
        },
        grid: {
          left: 0,
          right: 0,
          bottom: 0,
          top: 0,
        },
        series: [
          {
            type: 'line',
            data,
            itemStyle: {
              opacity: 0,
            },
            areaStyle: {
              color: 'skyblue',
            },
            smooth: true,
          },
        ],
      }
    },
    renderChart3(data) {
      //如果希望展示legend组件 这个组件默认会读取data中的name字段
      data.forEach((item) => {
        item.name = item.title + '|' + item.value
      })
      // 计算累计订单量
      let total = 0
      data.forEach((item) => {
        total += item.value
      })
      this.option3 = {
        title: [
          {
            text: '品类分布',
            textStyle: {
              fontSize: 14,
              color: '#666',
            },
            left: 20,
            top: 20,
          },
          {
            text: '累计订单量',
            subtext: total,
            x: '40%',
            y: '45%',
            textAlign: 'center',
            textStyle: {
              fontSize: 14,
              color: '#999',
            },
            subtextStyle: {
              fontSize: 28,
              color: '#333',
            },
          },
        ],
        series: {
          type: 'pie',
          name: '品类分布',
          data,
          radius: ['45%', '60%'],
          center: ['40%', '50%'],
          itemStyle: {
            borderWidth: 8,
            borderColor: '#fff',
          },
          label: {
            show: true,
            formatter(params) {
              //约定最终标签要显示的内容
              return params.data.title
            },
          },
        },
        tooltip: {
          formatter(params) {
            let res =
              params.seriesName +
              '<br/>' +
              params.marker +
              params.data.title +
              '<br/>' +
              params.marker +
              '销售额:' +
              params.data.value
            return res
          },
        },
        legend: {
          orient: 'vertical',
          left: '80%',
          textStyle: {
            color: '#888',
          },
        },
      }
    },
  },
  async mounted() {
    const res = await getKeywordData()
    this.totalData = res.data
    this.tableData = this.totalData.slice(0, 6)
    const data1 = this.totalData
      .map((item) => item.totalUser)
      .slice(0, 10)
      .reverse()
    this.renderChart1(data1)
    const data2 = this.totalData
      .map((item) => item.totalSearch)
      .slice(0, 10)
      .reverse()
    this.renderChart2(data2)
    const res2 = await getCategoryData()
    this.catagoryData = res2.data
    this.renderChart3(this.catagoryData.data1)
  },
}
</script>

<style lang="scss" scoped>
.third-comp {
  margin-top: 20px;
  display: flex;
  gap: 20px;
  & > div {
    flex: 1;
  }
  .left {
    .charts {
      margin:20px;
      display: flex;
      gap: 20px;
      & > div {
        flex: 1;
      }
      .echarts {
        height: 50px;
      }
      .title {
        color: #727171;
        font-size: 14px;
      }
      .value {
        font-size: 20px;
        font-weight: 600;
        margin-top: 10px;
      }
    }
    .table {
      .el-pagination {
        display: flex;
        justify-content: flex-end;
        margin-top: 16px;
      }
    }
  }
  .right {
    :deep(.el-card__header) {
      position: relative;
      .el-radio-group {
        position: absolute;
        top: 10%;
        right: 2%;
      }
    }
    :deep(.el-card__body) {
      box-sizing: border-box;
      height: 558px;
      .chart {
        height: 100%;
      }
    }
  }
}
.css-2 {
  margin-left: 8px;
  font-weight: 600;
}
</style>
